import apiClient from './axios'
import type { 
  Server, 
  ServerDetail, 
  ServerListParams, 
  ServerListResponse, 
  ReadmeResponse,
  Comment,
  Reply,
  ConfigJson,
  InstallCommand
} from '@/types/server'

export const getServers = async (params: ServerListParams = {}): Promise<ServerListResponse> => {
  // 处理tags数组参数
  const queryParams = { ...params }
  if (params.tags && Array.isArray(params.tags)) {
    queryParams.tags = params.tags.join(',')
  }
  
  // 确保page_size参数被正确传递
  if (params.page_size === undefined && params.pageSize !== undefined) {
    queryParams.page_size = params.pageSize
  }
  
  const response = await apiClient.get('/api/servers/', { params: queryParams })
  return response.data
}

export const getInternalServers = async (params: ServerListParams = {}): Promise<ServerListResponse> => {
  // 处理tags数组参数
  const queryParams = { ...params, is_internal: true }
  if (params.tags && Array.isArray(params.tags)) {
    queryParams.tags = params.tags.join(',')
  }
  
  // 确保page_size参数被正确传递
  if (params.page_size === undefined && params.pageSize !== undefined) {
    queryParams.page_size = params.pageSize
  }
  
  const response = await apiClient.get('/api/servers/', { params: queryParams })
  return response.data
}

export const getServerById = async (id: string): Promise<ServerDetail> => {
  const response = await apiClient.get(`/api/servers/${id}/`)
  return response.data
}

export const getServerReadme = async (id: string): Promise<ReadmeResponse> => {
  const response = await apiClient.get(`/api/servers/${id}/readme/`)
  return response.data
}

export const createServer = async (data: Partial<Server>): Promise<Server> => {
  const response = await apiClient.post('/api/servers/', data)
  return response.data
}

export const updateServer = async (id: string, data: Partial<Server>): Promise<Server> => {
  const response = await apiClient.patch(`/api/servers/${id}/`, data)
  return response.data
}

export const deleteServer = async (id: string): Promise<void> => {
  await apiClient.delete(`/api/servers/${id}/`)
}

// 评论相关API
export const getComments = async (serverId: string): Promise<Comment[]> => {
  const response = await apiClient.get(`/api/servers/${serverId}/comments/`)
  return response.data
}

export const createComment = async (serverId: string, content: string): Promise<Comment> => {
  const response = await apiClient.post(`/api/servers/${serverId}/comments/`, { content })
  return response.data
}

export const deleteComment = async (serverId: string, commentId: number): Promise<void> => {
  await apiClient.delete(`/api/servers/${serverId}/comments/${commentId}/`)
}

// 回复相关API
export const createReply = async (serverId: string, commentId: number, content: string): Promise<Reply> => {
  const response = await apiClient.post(`/api/servers/${serverId}/comments/${commentId}/replies/`, { content })
  return response.data
}

export const deleteReply = async (serverId: string, commentId: number, replyId: number): Promise<void> => {
  await apiClient.delete(`/api/servers/${serverId}/comments/${commentId}/replies/${replyId}/`)
}

// 配置JSON相关API
export const getConfigsJson = async (serverId: string): Promise<ConfigJson[]> => {
  const response = await apiClient.get(`/api/servers/${serverId}/configs/`)
  return response.data
}

export const createConfigJson = async (serverId: string, data: Partial<ConfigJson>): Promise<ConfigJson> => {
  const response = await apiClient.post(`/api/servers/${serverId}/configs/`, data)
  return response.data
}

// 安装命令相关API
export const getInstallCommands = async (serverId: string): Promise<InstallCommand[]> => {
  const response = await apiClient.get(`/api/servers/${serverId}/install-commands/`)
  return response.data
}

export const createInstallCommand = async (serverId: string, data: Partial<InstallCommand>): Promise<InstallCommand> => {
  const response = await apiClient.post(`/api/servers/${serverId}/install-commands/`, data)
  return response.data
} 